<%#
    File name : mtk_wifi_map_client_capabilities.htm
    This file is used in WebUI based on LuCI to display EasyMesh client capabilities.
%>
<%+header%>

<script src="/luci-static/resources/monCon.js"></script>

<h2><a>EasyMesh Client Capabilities</a></h2>
<div id="cli_db_display"></div>

<script type="text/javascript">

    function draw_cli_db_table(g_cli_db)
    {
        var disp_cli_idx = 1;
        var cli_db = g_cli_db['cli_db'];
        var cdb_tbl = document.createElement("TABLE");
        cdb_tbl.setAttribute("class", "cbi-section-table");
        var row, cell, th, th_txt;
        row = cdb_tbl.insertRow(-1);

        th = document.createElement("TH");
        th_txt = document.createTextNode("No.");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("MAC");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("BSSID");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Capability");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Phy Mode");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Max. BW 0");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Max. BW 1");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Spatial Streams");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Supported Bands");
        th.appendChild(th_txt);
        row.appendChild(th);

        th = document.createElement("TH");
        th_txt = document.createTextNode("Supported Channels");
        th.appendChild(th_txt);
        row.appendChild(th);

        for(var cli_idx = 0; cli_idx < parseInt(g_cli_db['num_clis']); cli_idx++){
            if(cli_db[cli_idx]['BSSID'] == '00:00:00:00:00:00'){
                continue;
            }
            row = cdb_tbl.insertRow(-1);
            cell = row.insertCell(-1);
            cell.innerHTML = disp_cli_idx++;
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['MAC'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['BSSID'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Capability'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Phy Mode'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Max. Bandwidth 0'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Max. Bandwidth 1'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Spatial Stream'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Known Band'];
            cell = row.insertCell(-1);
            cell.innerHTML = cli_db[cli_idx]['Known Channels'];
        }
        if(cdb_tbl.rows.length == 1){
            row = cdb_tbl.insertRow(-1);
            cell = row.insertCell(-1);
            cell.colSpan = cdb_tbl.rows[0].cells.length;
            cell.style.textAlign = "center";
            cell.innerHTML = "Client DB is empty!";
        }
        document.getElementById("cli_db_display").appendChild(cdb_tbl);
    }

    function get_client_db_cb(rsp)
    {
        try{
            var r = JSON.parse(rsp);
            if(r.status == "SUCCESS"){
                document.getElementById("cli_db_display").innerHTML = "";
                draw_cli_db_table(r);
            }
            else{
                console.log("Failed to get Client DB!\nStatus: ",r.status);
            }
            window.setTimeout(get_client_db, 10000);
        }
        catch(e){
            console.log("Incorrect response! Failed to get Client DB!",e.name,e.message);
            window.setTimeout(get_client_db, 10000);
        }
    }

    function get_client_db()
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "mtk", "multi_ap", "get_client_capabilities")%>', null,
            function(x)
            {
                console.log(x);
                get_client_db_cb(x.response);
            }
        );
    }

    window.onload = function(){
        MonCon.ping();
        document.getElementById("cli_db_display").innerHTML = "";
        window.setTimeout(get_client_db, 1);
    }

</script>
<%+footer%>